var treeGrid;
$(function(){
	treeGrid = $("#treeGrid").treegrid({
		title:"菜单列表管理",
		iconCls: 'icon-tip',
		rownumbers: true,
		animate: true,
		queryParams:{}, //查询条件  
		scrollbarSize:0,
		/*collapsible: true,*/
		fitColumns: true,
		url: contextPath + '/menu/getPrivilegeTreeData.action',
		method: 'POST',
		idField: 'id',
		treeField: 'name'
	});
	
	// 当窗口大小发生变化时，调整DataGrid的大小  
    $(window).resize(function() { 
    	//$('#searchPanel').panel('resize',{width:$(document.body).width() - 10});
        $('#treeGrid').resizeTreeGrid(300);  
    });
    
    //查询操作
    $("#query-btn").click(function(){
        var queryForm = $("#queryForm").serializeArray();
		var queryParams = $('#treeGrid').treegrid('options').queryParams;
        $.each(queryForm, function(i, field){
            queryParams[field.name] = field.value;
        });
        //重新加载treeGrid的数据  
        treeGrid.treegrid('load');
    });
	
	//添加
    $("#add-btn").click(function(){
    	showDialog("添加资源模块",contextPath+"/menu/toAddPrivilege.action");
    });
    
});

//修改
function editPrivilege(privilegeId){
	//treeGrid.treeGrid('clearSelections');
	showDialog("修改资源模块",contextPath+"/menu/toEditPrivilege.action?id="+privilegeId);
}

//删除
function delPrivilege(privilegeId){
	parent.$.messager.confirm('提示', '确定要删除吗？',function(r){
        if(r){
        	parent.$.messager.progress({
				title : '提示',
				text : '数据处理中，请稍后....'
			});
        	$.ajax({
                url: contextPath+'/menu/delPrivilege.action',
                type: 'post',
                data: "id="+privilegeId,
                error: function(){
                	parent.$.messager.progress('close');
                	parent.$.messager.alert('提示','操作失败，请重新再试！','error');      
                },
                success: function(data){
                    if (data.result == 'success') {
                    	parent.$.messager.alert('提示',data.msg,'info');
                    	//重新加载treeGrid的数据  
                    	treeGrid.treegrid('load');
                    }else{
                    	$.messager.alert('提示',data.msg,'error');      
                    }
                    parent.$.messager.progress('close');
                }
            });
        }
	});
}

function showDialog(title,url){
	parent.$.modalDialog({
		title : title,
		iconCls:"icon-tip",
		width:600,
		height:350,
		href : url,
		buttons:[{ 
			text : '确定',
			iconCls : 'icon-ok',
			handler : function() {
				parent.$.modalDialog.openner_treeGrid = treeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
				var f = parent.$.modalDialog.handler.find('#form');
				f.submit();
			}
		}, {
			text : '取消',
			iconCls : 'icon-cancel',
			handler : function() {
				parent.$.modalDialog.handler.dialog('close');
		   } 
		}]
	});
}

//资源类型
function formatOperate(value,rowData){
	var privilegeId = rowData.id;
	var optTemplate = "<a class=\"btn btn-mini ml5\" href=\"javascript:editPrivilege('"+privilegeId+"');\"><i class=\"icon-edit-data\"></i>修改</a>";
	   optTemplate += "<a class=\"btn btn-mini ml5\" href=\"javascript:delPrivilege('"+privilegeId+"');\"><i class=\"icon-remove-data\"></i>删除</a>";
	return optTemplate;
}
//资源类型
function formatMenu(value,rowData){
	if("1"==value){
		return "菜单";
	}else if("2"==value){
		return "功能";
	}else{
		return "资源";
	}
}